package com.chijun.mform.full.view;

import static com.chijun.mform.create.fragment.FormAddProcessFragment.APPROVAL_SELECT_MODE_FA_QI_REN_XUAN_ZE;
import static com.chijun.mform.create.fragment.FormAddProcessFragment.APPROVAL_SELECT_MODE_FA_QI_REN_ZI_JI;
import static com.chijun.mform.create.fragment.FormAddProcessFragment.APPROVAL_SELECT_MODE_ZHI_DING;
import static com.chijun.mform.create.fragment.FormAddProcessFragment.CHAO_SONG_REN_JIE_DIAN;
import static com.chijun.mform.create.fragment.FormAddProcessFragment.FA_QI_REN_JIE_DIAN;
import static com.chijun.mform.create.fragment.FormAddProcessFragment.SHEN_PI_REN_JIE_DIAN;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.R;
import com.chijun.mform.create.dialog.ISelectProcessPerson;
import com.chijun.mform.create.dialog.SelectPersonNewDialog;
import com.chijun.mform.create.mode.FormApprovePerson;
import com.chijun.mform.create.mode.FormApproveProcess;
import com.example.ccj.b.c.custom.MImageView;
import com.example.ccj.b.c.custom.MRoundCornerImageView;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.b.c.custom.tree.TreeMode;
import com.example.ccj.b.c.custom.tree.tree2.ISelectedTreeDataResult;
import com.example.ccj.b.c.custom.tree.tree2.ShowSelectedTreeDialog;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ThreadUtils;
import com.example.ccj.u.ThreadUtilsListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 显示审批view
 * 2024年11月10日23:38:06
 */
public class AdapterLoadFormApproveAdapter extends RecyclerView.Adapter<AdapterLoadFormApproveAdapter.ViewHolder>{

    List<FormApproveProcess> formFieldList;
    Map<Long,List<FormApprovePerson>> formApprovePersonMap;

    /**
     * 字段类型数组
     *
     * @param formFieldList 审批节点
     * @param formApprovePersonMap 审批节点的人员
     */
    public AdapterLoadFormApproveAdapter(List<FormApproveProcess> formFieldList, Map<Long,List<FormApprovePerson>> formApprovePersonMap){
       this.formFieldList = formFieldList;
       this.formApprovePersonMap = formApprovePersonMap;
        selectPeronMap = new HashMap<>();
    }

    @NonNull
    @Override
    public AdapterLoadFormApproveAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AdapterLoadFormApproveAdapter.ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_load_form_approve, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterLoadFormApproveAdapter.ViewHolder holder, int position) {
        FormApproveProcess formApproveProcess = formFieldList.get(position);
        holder.name.setText(formApproveProcess.name);

        //左侧导航线显示设置
        //第一个不显示上面的导航线
        if (position == 0) holder.leftLineTop.setVisibility(View.INVISIBLE);
        else holder.leftLineTop.setVisibility(View.VISIBLE);
        //最后一个不显示下面的导航线
        if (position == formFieldList.size() - 1) holder.leftLineBottom.setVisibility(View.INVISIBLE);
        else holder.leftLineBottom.setVisibility(View.VISIBLE);

        //审批节点名称设置
        if (StringUtils.isEmpty(formApproveProcess.name)) {
            if (formApproveProcess.type == SHEN_PI_REN_JIE_DIAN) {
                holder.name.setText("审批人");
            } else if (formApproveProcess.type == CHAO_SONG_REN_JIE_DIAN) {
                holder.name.setText("抄送人");
            } else if (formApproveProcess.type == FA_QI_REN_JIE_DIAN) {
                holder.name.setText("发起人");
            }
        } else {
            holder.name.setText(formApproveProcess.name);
        }

        holder.hint.setText(String.valueOf(formApproveProcess.id));

        //审批人设置
        List<FormApprovePerson> formApprovePeople = formApprovePersonMap.get(formApproveProcess.id);

        final int po = position;
        holder.onePersonZhiShi.setVisibility(View.VISIBLE);

        //审批人为指定时，加载审批人
        if (formApproveProcess.personType == APPROVAL_SELECT_MODE_ZHI_DING) {
            //如果没有审批人 隐藏所有审批人按钮以及显示最后一个审批人按钮 显示添加按钮
            if (formApprovePeople == null || formApprovePeople.isEmpty()) {
                holder.allPersonLayout.setVisibility(View.GONE);
                holder.showPersonLayout.setVisibility(View.GONE);
                holder.addPersonLayout.setVisibility(View.VISIBLE);
                holder.addPerson.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addPersonClick(holder.addPerson,"审批人",po,formApproveProcess);
                    }
                });
                //如果有一个审批人 隐藏所有审批人按钮，显示最后一个审批人按钮
            } else if (formApprovePeople.size() == 1) {
                holder.allPersonLayout.setVisibility(View.GONE);
                holder.showPersonLayout.setVisibility(View.VISIBLE);
                holder.addPersonLayout.setVisibility(View.GONE);
                holder.addPersonLayout.setOnClickListener(null);
                holder.onePersonZhiShi.setVisibility(View.GONE);
                //审批人名称
                holder.showPersonName.setText(formApprovePeople.get(0).personName);
                //审批人图片
                if (StringUtils.isEmpty(formApprovePeople.get(0).image)) {
                    holder.showPerson.setImageResource(R.drawable.form_approve_one_person);
                } else {
                    ImageUtils.loadImage(holder.showPerson.getContext(),formApprovePeople.get(0).image,holder.showPerson);
                }
                //多个审批人 显示所有审批人按钮，显示最后一个审批人按钮 显示添加按钮
            } else {
                holder.allPersonLayout.setVisibility(View.VISIBLE);
                holder.showPersonLayout.setVisibility(View.VISIBLE);
                holder.addPersonLayout.setVisibility(View.GONE);
                holder.onePersonZhiShi.setVisibility(View.GONE);

                holder.allPerson.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showAllPerson(holder.addPerson,formApproveProcess,po,false);
                    }
                });

                //审批人名称
                holder.showPersonName.setText(formApprovePeople.get(formApprovePeople.size()-1).personName);
                //审批人图片
                if (StringUtils.isEmpty(formApprovePeople.get(formApprovePeople.size()-1).image)) {
                    holder.showPerson.setImageResource(R.drawable.form_approve_one_person);
                } else {
                    ImageUtils.loadImage(holder.showPerson.getContext(),formApprovePeople.get(formApprovePeople.size()-1).image,holder.showPerson);
                }
            }
        }

        //发起人选择审批人
        if (formApproveProcess.personType == APPROVAL_SELECT_MODE_FA_QI_REN_XUAN_ZE) {
            if (formApprovePeople == null || formApprovePeople.isEmpty()) {
                holder.allPersonLayout.setVisibility(View.GONE);
                holder.showPersonLayout.setVisibility(View.GONE);
                holder.addPersonLayout.setVisibility(View.VISIBLE);
                holder.addPerson.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addPersonClick(holder.addPerson,"审批人",po,formApproveProcess);
                    }
                });
            } else if (formApprovePeople.size() == 1) {
                if (formApproveProcess.personNum == 0 || formApproveProcess.personNum > 1) {
                    holder.addPersonLayout.setVisibility(View.VISIBLE);
                    holder.addPerson.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            addPersonClick(holder.addPerson,"审批人",po,formApproveProcess);
                        }
                    });
                } else {
                    holder.addPersonLayout.setVisibility(View.GONE);
                    holder.addPersonLayout.setOnClickListener(null);
                    holder.onePersonZhiShi.setVisibility(View.GONE);
                    holder.showPerson.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            addPersonClick(holder.showPerson,"审批人",po,formApproveProcess);
                        }
                    });
                }

                holder.allPersonLayout.setVisibility(View.GONE);
                holder.showPersonLayout.setVisibility(View.VISIBLE);
                //审批人名称
                holder.showPersonName.setText(formApprovePeople.get(0).personName);
                //审批人图片
                if (StringUtils.isEmpty(formApprovePeople.get(0).image)) {
                    holder.showPerson.setImageResource(R.drawable.form_approve_one_person);
                } else {
                    ImageUtils.loadImage(holder.showPerson.getContext(),formApprovePeople.get(0).image,holder.showPerson);
                }
            } else {
                holder.allPersonLayout.setVisibility(View.VISIBLE);
                holder.showPersonLayout.setVisibility(View.VISIBLE);
                holder.allPerson.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showAllPerson(holder.allPerson,formApproveProcess,po,true);
                    }
                });

                //审批人名称
                holder.showPersonName.setText(formApprovePeople.get(formApprovePeople.size()-1).personName);
                //审批人图片
                if (StringUtils.isEmpty(formApprovePeople.get(formApprovePeople.size()-1).image)) {
                    holder.showPerson.setImageResource(R.drawable.form_approve_one_person);
                } else {
                    ImageUtils.loadImage(holder.showPerson.getContext(),formApprovePeople.get(formApprovePeople.size()-1).image,holder.showPerson);
                }

                //是否可添加
                if (formApproveProcess.personNum == 0 || formApprovePeople.size() < formApproveProcess.personNum) {
                    holder.addPersonLayout.setVisibility(View.VISIBLE);
                    holder.onePersonZhiShi.setVisibility(View.VISIBLE);
                    holder.addPerson.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            addPersonClick(holder.addPerson,"审批人",po,formApproveProcess);
                        }
                    });
                } else {
                    holder.onePersonZhiShi.setVisibility(View.GONE);
                    holder.addPersonLayout.setVisibility(View.GONE);
                }
            }
        }

        //发起人自己
        if (formApproveProcess.personType == APPROVAL_SELECT_MODE_FA_QI_REN_ZI_JI) {
            holder.allPersonLayout.setVisibility(View.GONE);
            holder.addPersonLayout.setVisibility(View.GONE);
            holder.showPersonLayout.setVisibility(View.VISIBLE);
            holder.onePersonZhiShi.setVisibility(View.GONE);
            holder.showPerson.setOnClickListener(null);
            holder.showPerson.setImageResource(R.drawable.form_approve_one_person);
            holder.showPersonName.setText("自己");
        }
    }


    private void addPersonClick(View view,String titleName,int position,FormApproveProcess formApproveProcess){
        SelectPersonNewDialog selectPersonNewDialog = new SelectPersonNewDialog(view.getContext(), titleName, selectPeronMap.get(formApproveProcess.id),formApproveProcess.personNum, false,false, new ISelectProcessPerson() {
            @Override
            public void onOver(List<TreeMode> treeModeList) {
                resetPerson(treeModeList,formApproveProcess,position);
            }
        });
        selectPersonNewDialog.show();
    }

    private Map<Long,List<String>> selectPeronMap;


    private void showAllPerson(View v,FormApproveProcess formApproveProcess,int position,boolean isCanCon){
        List<FormApprovePerson> formApprovePeople = formApprovePersonMap.get(formApproveProcess.id);
        List<TreeMode> treeModeList = new ArrayList<>();
        if (formApprovePeople != null) {
            for (FormApprovePerson formApprovePerson : formApprovePeople) {
                TreeMode treeMode = new TreeMode(formApprovePerson.personId,null,formApprovePerson.personName,TreeMode.DATA_TYPE_DATA);
                treeMode.isChecked = true;
                treeModeList.add(treeMode);
            }
        }

        ShowSelectedTreeDialog dialog = new ShowSelectedTreeDialog(v.getContext(), treeModeList, isCanCon,new ISelectedTreeDataResult() {
            public void onResult() {
                //人员有变动后回调
                resetPerson(treeModeList,formApproveProcess,position);
            }
        });
        dialog.show();
    }


    private void resetPerson(List<TreeMode> treeModeList,FormApproveProcess formApproveProcess,int position){
        ThreadUtils.executeByCpu(new ThreadUtilsListener<Object>() {
            @Override
            public Object doInBackground() throws Throwable {
                List<FormApprovePerson> formApprovePeople = new ArrayList<>();
                List<String> stringList = selectPeronMap.get(formApproveProcess.id);
                if (stringList == null) {
                    stringList = new ArrayList<>();
                    selectPeronMap.put(formApproveProcess.id,stringList);
                }
                else stringList.clear();

                for (TreeMode treeMode : treeModeList) {
                    stringList.add(treeMode.dataId);
                    FormApprovePerson person = new FormApprovePerson();
                    person.personId = treeMode.dataId;
                    person.personName = treeMode.name;
                    person.processId = formApproveProcess.id;
                    formApprovePeople.add(person);
                }
                formApprovePersonMap.put(formApproveProcess.id,formApprovePeople);
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                notifyItemChanged(position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return formFieldList == null ? 0 : formFieldList.size();
    }


    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final MTextView name,hint,showPersonName;
        private final MImageView allPerson,addPerson,onePersonZhiShi;
        private final MRoundCornerImageView showPerson;
        private final LinearLayout showPersonLayout,allPersonLayout,addPersonLayout;
        private final View leftLineTop,leftLineBottom,leftLineCenter;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            leftLineTop = itemView.findViewById(R.id.adapter_load_form_approve_left_line_top);
            leftLineBottom = itemView.findViewById(R.id.adapter_load_form_approve_left_line_bottom);
            leftLineCenter = itemView.findViewById(R.id.adapter_load_form_approve_left_line_center);
            name = itemView.findViewById(R.id.adapter_load_form_approve_name);
            hint = itemView.findViewById(R.id.adapter_load_form_approve_hint);
            allPersonLayout = itemView.findViewById(R.id.adapter_load_form_approve_all_person_layout);
            allPerson = itemView.findViewById(R.id.adapter_load_form_approve_all_person);
            showPersonLayout = itemView.findViewById(R.id.adapter_load_form_approve_one_person_layout);
            showPerson = itemView.findViewById(R.id.adapter_load_form_approve_one_person);
            onePersonZhiShi = itemView.findViewById(R.id.adapter_load_form_approve_one_person_zhi_shi);
            showPersonName = itemView.findViewById(R.id.adapter_load_form_approve_one_person_name);
            addPersonLayout = itemView.findViewById(R.id.adapter_load_form_approve_add_person_layout);
            addPerson = itemView.findViewById(R.id.adapter_load_form_approve_add_person);
        }
    }
}
